/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-04
 * Time: 17:16
 */

/**
 *
 * 懒汉模式
 */
class SingletonLazy{
    private static  volatile SingletonLazy instance = null;
    public  static  SingletonLazy getInstance() {
        Object locker = new Object();
        if (instance == null) {//判断是否要加锁

            synchronized (locker) {
                //synchronized 会阻塞在线程,阻塞过程中,其他线程就可能修改instance的值
                if (instance == null)  {     //判断是否要创建对象,
                    instance = new SingletonLazy();

                    /**
                     * 这一行代码,可以分为三个步骤:
                     * 1:申请内存空间
                     * 2:调用构造方法
                     * 3:把此时内存空间的地址,赋值给instance引用
                     */
                }

            }
        }
        return instance;
    }
    private SingletonLazy(){

    }
}
public class Demo2 {
    public static void main(String[] args) {
        SingletonLazy s1=SingletonLazy.getInstance();
        SingletonLazy s2=SingletonLazy.getInstance();
        System.out.println(s1==s2);

    }
}
